<template>
  <div class="announcement-details-container">
    <el-container>
      <el-header class="header">
        <div class="header-content">
          <span class="header-title">公告详情</span>
          <el-button class="back-button" @click="goBack" type="primary" icon="el-icon-arrow-left">返回</el-button>
        </div>
      </el-header>
      <el-main class="main">
        <div class="announcement-detail">
          <h1>{{ announcementDetail.title }}</h1>
          <div class="divider">
            <el-divider></el-divider>
            <div class="info">
              <!-- <span class="author">作者：{{ announcementDetail.creatorName }}</span>
              <span class="spacer"></span> -->
              <span class="time">发布时间: {{ announcementDetail.createTime }}</span>
            </div>
            <el-divider></el-divider>
          </div>
          <div v-html="announcementDetail.content" class="announcement-content"></div>
        </div>
      </el-main>
      <el-footer class="footer">
        <div class="footer-navigation" style="display: flex; justify-content: space-between;">
          <el-button @click="preAnnouncement" :disabled="!hasPreAnnouncement">〈 上一篇</el-button>
          <el-button @click="nextAnnouncement" :disabled="!hasNextAnnouncement">下一篇 〉</el-button>
        </div>
      </el-footer>
    </el-container>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from "vue";
import { useRoute } from "vue-router";
import { getAnnouncementDetailService, preAnnouncementService, nextAnnouncementService } from "@/apis/announcement";
import router from "@/router";

function goBack() {
  router.go(-1);
}

const announcementDetail = reactive({});
const hasPreAnnouncement = ref('');
const hasNextAnnouncement = ref('');
let announcementId = useRoute().query.announcementId;

async function getAnnouncementDetail() {
  const res = await getAnnouncementDetailService(announcementId);
  Object.assign(announcementDetail, res.data);
  announcementId = res.data.announcementId;
}

async function getPre() {
  const preRes = await preAnnouncementService(announcementId);
  hasPreAnnouncement.value = preRes.data;
}
async function getNext() {
  const preRes = await nextAnnouncementService(announcementId);
  hasNextAnnouncement.value = preRes.data;
}


onMounted(() => {
  getAnnouncementDetail();
  getPre();
  getNext();
});

async function preAnnouncement() {
  if (hasPreAnnouncement.value) {
    console.log("pre", hasPreAnnouncement);
    hasNextAnnouncement.value = announcementId;
    announcementId = hasPreAnnouncement.value;
    getAnnouncementDetail(announcementId);
    getPre();
  }
}

async function nextAnnouncement() {
  if (hasNextAnnouncement.value) {
    hasPreAnnouncement.value = announcementId;
    announcementId = hasNextAnnouncement.value;
    getAnnouncementDetail(announcementId);
    getNext();
  }
}
</script>

<style lang="scss" scoped>
.announcement-details-container {
  background-color: #f0f4ff;
  height: 100%;
  width: 100%;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;

  :deep(.el-container) {
    height: 100%;
    display: flex;
    flex-direction: column;
    margin: 0;
    padding: 0;
  }

  .header {
    height: 60px;
    display: flex;
    align-items: center;
    background-color: #007bff;
    color: white;
    padding: 0;
    margin: 0;
    flex-shrink: 0;

    .header-content {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 20px;
      width: 100%;

      .header-title {
        font-size: 20px;
      }

      .back-button {
        margin-left: auto;
      }
    }
  }

  .main {
    display: flex;
    justify-content: center;
    padding: 10px;
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    min-height: calc(100vh - 120px); /* 减去header和footer的高度 */

    .announcement-detail {
      min-height: calc(100vh - 160px); /* 适当减少高度以确保边距 */
      width: 100%;
      text-align: center;
      background-color: white;
      border-radius: 8px;
      box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
      padding: 20px;

      h1 {
        font-size: 24px;
        margin: 20px 0;
      }

      .divider {
        margin: 20px 0;

        .info {
          display: flex;
          justify-content: space-between;
          width: 100%;
          padding: 0 10px;
          font-size: 14px;
          color: #555;

          .spacer {
            flex-grow: 1;
            /* Spacer to create space between author and time */
          }
        }
      }

      .announcement-content {
        text-align: left;
        margin-bottom: 20px;

        p {
          font-size: 16px;
        }
      }
    }
  }

  .footer {
    height: 60px;
    display: flex;
    justify-content: center;
    padding: 10px;
    flex-shrink: 0;

    .footer-navigation {
      display: flex;
      justify-content: space-between;
      width: 66%;

      .el-button {
        width: 48%;
      }
    }
  }
}
</style>